package org.example.translate_braille.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.translate_braille.entity.Photo;
import org.example.translate_braille.entity.User;
import org.example.translate_braille.mapper.UserMapper;
import org.example.translate_braille.resdata.DataEnum;
import org.example.translate_braille.resdata.X;
import org.example.translate_braille.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jacky
 * @since
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserService userService;

    @Override
    public X showUserList(User user) {
        try {
            //构造器
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(User::getIsDeleted, 0)
            ;

            //分页查询
            List<User> userList= userService.list(queryWrapper);
            return X.ResData(DataEnum.SUCCESS, userList);
        }catch(Exception e){
            return X.ResData(DataEnum.SELECT_FAIL, Convert.toStr(e));
        }
    }

    @Override
    public X<User> showUser(String openId) {
        try {
            //构造器
            QueryWrapper<User> queryable = new QueryWrapper<>();
            queryable.lambda()
                    .eq(User::getOpenId, openId)
                    .eq(User::getIsDeleted,0)
            ;
            return X.ResData(DataEnum.SUCCESS, userService.list(queryable));
        }catch(Exception e){
            return X.ResData(DataEnum.SELECT_FAIL, Convert.toStr(e));
        }
    }

    @Override
    public X inOrUpUser(User user) {
        try{
            Boolean isFlag = userService.saveOrUpdate(user);
            return X.ResData(DataEnum.SUCCESS,isFlag);
        }catch (Exception e){
            return X.ResData(DataEnum.UPDATE_FAIL,Convert.toStr(e));
        }
    }

    @Override
    public X deleteUser(User user) {
        try{
            List<String> stringList = Arrays.asList(user.getId().split(","));
            return X.ResData(DataEnum.SUCCESS,userService.removeByIds(stringList));
        }catch (Exception e){
            return X.ResData(DataEnum.DELETE_FAIL,Convert.toStr(e));
        }
    }
}
